package com.xuexiang.xuidemo.slice.button;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import com.xuexiang.xui_lib.component.button.shinebutton.ShineButton;
import com.xuexiang.xui_lib.component.progress.loading.ARCLoadingView;
import com.xuexiang.xui_lib.popup.GoodComponent;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.DisplayUtils;
import com.xuexiang.xuidemo.util.ToastUtil;

import java.io.IOException;

/**
 * 增强效果的按钮，自带闪烁的特效
 */
public class ShineButtonSlice extends BaseAbilitySlice {


    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_shine_button_slice;
    }

    @Override
    protected int getTitleBarId() {
        return ResourceTable.Id_title_bar;
    }

    @Override
    protected void initComponents() {
        super.initComponents();

        ShineButton shineButton1 = (ShineButton) findComponentById(ResourceTable.Id_shine_button1);
        ShineButton shineButton2 = (ShineButton) findComponentById(ResourceTable.Id_shine_button2);
        ShineButton shineButton3 = (ShineButton) findComponentById(ResourceTable.Id_shine_button3);
        ShineButton shineButton4 = (ShineButton) findComponentById(ResourceTable.Id_shine_button4);


        shineButton1.setAddRootViewListener(() -> {
            return (DependentLayout) findComponentById(ResourceTable.Id_root_view1);
        });
        shineButton2.setAddRootViewListener(() -> {
            return (DependentLayout) findComponentById(ResourceTable.Id_root_view2);
        });
        shineButton3.setAddRootViewListener(() -> {
            return (DependentLayout) findComponentById(ResourceTable.Id_root_view3);
        });
        shineButton4.setAddRootViewListener(() -> {
            return (DependentLayout) findComponentById(ResourceTable.Id_root_view4);
        });


        shineButton2.setOnCheckStateChangeListener((shineButton, isChecked) -> {
            new EventHandler(EventRunner.getMainEventRunner()).postTask(()->{
                ToastUtil.showToast(getContext(), "checked:" + isChecked);
            },1000);

        });

        findComponentById(ResourceTable.Id_dialog_btn).setClickedListener(component -> {
            new ShineDialog(getContext()).show();
        });
    }


    static class ShineDialog extends CommonDialog {
        Context context;

        public ShineDialog(Context context) {
            super(context);
            this.context = context;
            init();
        }


        private void init() {
            Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_xui_layout_shine_dialog_view, null, true);
            ShineButton shineButton1 = (ShineButton) component.findComponentById(ResourceTable.Id_shine_button1);
            ShineButton shineButton2 = (ShineButton) component.findComponentById(ResourceTable.Id_shine_button2);
            ShineButton shineButton3 = (ShineButton) component.findComponentById(ResourceTable.Id_shine_button3);
            ShineButton shineButton4 = (ShineButton) component.findComponentById(ResourceTable.Id_shine_button4);

            shineButton1.setAddRootViewListener(() -> {
                return (DependentLayout) component.findComponentById(ResourceTable.Id_root_view1);
            });
            shineButton2.setAddRootViewListener(() -> {
                return (DependentLayout) component.findComponentById(ResourceTable.Id_root_view2);
            });
            shineButton3.setAddRootViewListener(() -> {
                return (DependentLayout) component.findComponentById(ResourceTable.Id_root_view3);
            });
            shineButton4.setAddRootViewListener(() -> {
                return (DependentLayout) component.findComponentById(ResourceTable.Id_root_view4);
            });

            setSize(DisplayUtils.getDisplayWidthInPx(context)-DisplayUtils.getDisplayWidthInPx(context)/8, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            setAlignment(LayoutAlignment.CENTER);
            setContentCustomComponent(component);


            component.findComponentById(ResourceTable.Id_text_no).setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    hide();
                }
            });

            component.findComponentById(ResourceTable.Id_text_ok).setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    hide();
                }
            });
        }

    }


}
